package com.swanclouds.redfish.systems;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.swanclouds.api.Apis;
import com.swanclouds.api.chassis.NetworkAdaptersService;
import com.swanclouds.api.systems.NetworkInterfaceService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseSystemService;
import com.swanclouds.redfish.systems.domain.NetworkInterface;

import java.util.List;

/**
 * @author 张立强
 * @version jdk1.8.0
 */
public class NetworkInterfaceServiceImpl extends BaseSystemService implements NetworkInterfaceService {
    /**
     * @return 当前网络接口集合资源信息
     */
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class, uri(ClientConstants.SYSTEMS_NETWORK_INTERFACES, getSystemId())).execute();
    }

    /**
     * 网络接口资源信息
     *
     * @param networkInterfaceId 网络接口ID
     * @return the NetworkInterface info
     */
    @Override
    public NetworkInterface getNetworkInterface(String networkInterfaceId) {
        Preconditions.checkNotNull(networkInterfaceId, "NetworkInterface Id is null");
        return get(NetworkInterface.class, uri(ClientConstants.SYSTEMS_NETWORK_INTERFACES_ID, getSystemId(), networkInterfaceId)).execute();
    }

    /**
     * @return 网络接口资源信息集合
     */
    @Override
    public List<NetworkInterface> list() {
        List<NetworkInterface> networkInterfaceList = Lists.newArrayList();
        CollectionResource resource = get();
        resource.getCollectionResourceList().stream().forEach(ref -> {
            networkInterfaceList.add(get(NetworkInterface.class, uri(ref)).execute());
        });
        return networkInterfaceList;
    }

    /**
     * @return 网络端口集合资源服务
     */
    @Override
    public NetworkAdaptersService NetworkAdapters() {
        return Apis.get(NetworkAdaptersService.class);
    }

}
